﻿namespace SERFLITEMAX20
{
    using SERFLITEMAX20.Core;
    using SERFLITEMAX20.SocketLib;
    using System;
    using System.Diagnostics;
    using System.Threading;

    internal class Program
    {
        private static Thread mSearchThread = null;
        private static Server mServer = null;
        private static Thread mThread = null;

        private static void Main(string[] args)
        {
            Console.WriteLine("Welcom to SERFLITEMAX20");
            Console.WriteLine("waiting for wow...");
            while (Process.GetProcessesByName("WoW").Length == 0)
            {
                if (Console.ReadKey(true).Key == ConsoleKey.Escape)
                {
                    return;
                }
            }
            Console.WriteLine("wow found now starting");
            SERFLITEMAX20.Core.Main main = new SERFLITEMAX20.Core.Main();
            main.AfterUpdate += new Main.OnUpdateEvent(Program.Update);
            main.Start();
            Console.WriteLine("SERFLITEMAX20 started");
            while (main.HasStarted)
            {
                Console.ReadLine();
                break;
            }
        }

        private static void SearchMemory(object sender)
        {
            ((SERFLITEMAX20.Core.Main)sender).Memory.FindString("Wooden Chair");
        }

        private static void SendXml(object sender)
        {
            try
            {
                mServer.SendMessage(((SERFLITEMAX20.Core.Main)sender).GetXML());
            }
            catch (Exception exception)
            {
                Console.WriteLine(exception.Message);
            }
        }

        private static void Update(SERFLITEMAX20.Core.Main sender)
        {
            if (mThread != null)
            {
                if (mThread.ThreadState == System.Threading.ThreadState.Running)
                {
                    return;
                }
                try
                {
                    mThread.Abort();
                }
                catch
                {
                }
            }
            if (!((mServer != null) && mServer.continueUpdate))
            {
                mServer = new Server();
                mServer.Start();
            }
            mThread = new Thread(new ParameterizedThreadStart(Program.SendXml));
            mThread.Start(sender);
        }

        private static void UpdateStatus(string pStatus, bool attention)
        {
            Console.WriteLine(pStatus);
        }
    }
}

